﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Odbc;
using System.Web;

namespace WLMCoreLibrary
{
    public class SubDomain : BaseClass
    {
        #region private fields
        private String _nombre;
        #endregion

        #region public properties
        public String nombre { get { return _nombre; } set { _nombre = value; } }
        #endregion

        #region constructors
        public SubDomain(ref dbConnector ConexionDB)
        {
            
            WLMClassBinder classBinder = new WLMClassBinder(ref ConexionDB);
            id = classBinder.NewRecord("subDominios");
            string queryString = "";
            queryString = "SELECT * FROM subDominios WHERE id=\"" + id + "\"";
            GetRecord(queryString);
        }

        public SubDomain(int registryId,ref dbConnector ConexionDB)
        {
            
            string queryString = "";
            queryString = "SELECT * FROM subDominios WHERE id='" + registryId + "'";
            WLMClassBinder classBinder = new WLMClassBinder(ref ConexionDB);
            this.GetRecord(queryString);
        }
        #endregion

        #region public methods



        public void Delete()
        {
            string queryString = "";
            queryString = "DELETE FROM subDominios WHERE id=\"" + id + "\"";
            ConexionDBInterna.ExecuteCommand(queryString);

        }
        #endregion


        #region Private Methods
        private void GetRecord(string queryString)
        {
            OdbcDataReader reader = ConexionDBInterna.ExecuteReader(queryString);
            if (reader.HasRows)
            {
                if (!reader.IsDBNull(reader.GetOrdinal("id")))
                    id = (System.Int32)reader["id"];
                if (!reader.IsDBNull(reader.GetOrdinal("fechaCreacion")))
                    fechaCreacion = Convert.ToDateTime(reader["fechaCreacion"]); if (!reader.IsDBNull(reader.GetOrdinal("fechaModificacion"))) fechaModificacion = Convert.ToDateTime(reader["fechaModificacion"]); if (!reader.IsDBNull(reader.GetOrdinal("idUsuarioModificacion"))) idUsuarioModificacion = (System.Int32)reader["idUsuarioModificacion"];


                if (!reader.IsDBNull(reader.GetOrdinal("idUsuario")))
                    idUsuario = (System.Int32)reader["idUsuario"];
                if (!reader.IsDBNull(reader.GetOrdinal("nombre")))
                    _nombre = (System.String)reader["nombre"];
            }
        }


        protected override void UpdateBind()
        {
            StringBuilder strCommand = new StringBuilder();
            strCommand.Append("UPDATE subDominios set "); strCommand.Append("fechaModificacion='" + this.fechaModificacion.ToString("yyyy-MM-dd HH:mm:ss") + "'"); strCommand.Append(",idUsuarioModificacion=\"" + idUsuarioModificacion.ToString() + "\"");
            strCommand.Append("fechaModificacion='" + DateTime.UtcNow.AddHours(-6).ToString("yyyy-MM-dd HH:mm:ss") + "'");
            strCommand.Append(",nombre=\"" + this._nombre.ToString() + "\"");
            strCommand.Append(" WHERE id=\"" + id + "\"");
            ConexionDBInterna.ExecuteCommand(strCommand.ToString());
        }
        #endregion
    }

    public class SubDomainCollection
    {
        private dbConnector ConexionDB;
        private BaseClassArray<SubDomain> _subdomains;

        public BaseClassArray<SubDomain> SubDomainList { get { return _subdomains; } }

        public SubDomainCollection(ref dbConnector dbConn)
        {
            
            string strCommand = "SELECT id FROM subdominios order by id";

            OdbcDataReader reader = ConexionDB.ExecuteReader(strCommand);
            _subdomains = new BaseClassArray<SubDomain>();
            while (reader.Read())
            {
                SubDomain newEntry = new SubDomain(Convert.ToInt32(reader["id"]),ref ConexionDB);

                _subdomains.Add(newEntry);
            }
            reader.Close();
            reader.Dispose();
        }
    }

    public class MiWebSiteDomain : BaseClass
    {
       #region private fields
        protected String _dominio;
        protected String _logotipo;
        protected String _slogan;
        protected String _estadisticas;
        protected String _keywords;
        protected String _description;
        protected String _mapPoint;
        protected String _mapsApiKey;
        protected String _twitterAccount;
        protected String _twitterPassword;
        protected int _idDomain;
        protected int _idParentDomain;
        protected int _idGrandParentDomain;
        protected String _bitlyApiKey;
        protected String _longName;
        private bool _activo;
        private String _oauth_token;
        private String _oauth_token_secret;
        private int _idTwitterApplication;
        #endregion

        #region public properties
        public String dominio { get { return _dominio; } set { _dominio = value; } }
        public String logotipo { get { return _logotipo; } set { _logotipo = value; } }
        public String slogan { get { return _slogan; } set { _slogan = value; } }
        public String estadisticas { get { return _estadisticas; } set { _estadisticas = value; } }
        public String keywords { get { return _keywords; } set { _keywords = value; } }
        public String description { get { return _description; } set { _description = value; } }
        public String mapPoint { get { return _mapPoint; } set { _mapPoint = value; } }
        public String mapsApiKey { get { return _mapsApiKey; } set { _mapsApiKey = value; } }
        public String twitterAccount { get { return _twitterAccount; } set { _twitterAccount = value; } }
        public String twitterPassword { get { return _twitterPassword; } set { _twitterPassword = value; } }
        public int idDomain { get { return _idDomain; } set { _idDomain = value; } }
        public int idParentDomain { get { return _idParentDomain; } set { _idParentDomain = value; } }
        public int idGrandParentDomain { get { return _idGrandParentDomain; } set { _idGrandParentDomain = value; } }
        public String bitlyApiKey { get { return _bitlyApiKey; } set { _bitlyApiKey = value; } }
        public String longName { get { return _longName; } set { _longName = value; } }
        public bool activo { get { return _activo; } set { _activo = value; } }
        public String oauth_token { get { return _oauth_token; } set { _oauth_token = value; } }
        public String oauth_token_secret { get { return _oauth_token_secret; } set { _oauth_token_secret = value; } }
        public int idTwitterApplication { get { return _idTwitterApplication; } set { _idTwitterApplication = value; } }
        #endregion

        #region constructors
        public MiWebSiteDomain(ref dbConnector ConexionDB)
        {
            ConexionDBInterna = ConexionDB;
            WLMClassBinder classBinder = new WLMClassBinder(ref ConexionDB);
            id = classBinder.NewRecord("DOMINIOS");
            string queryString = "";
            queryString = "SELECT * FROM DOMINIOS WHERE id='" + id + "'";
            GetRecord(queryString);
        }

        public MiWebSiteDomain(string dom,ref dbConnector ConexionDB)
        {
            ConexionDBInterna = ConexionDB;
            string queryString = "";
            queryString = "SELECT * FROM DOMINIOS WHERE dominio='" + dom + "' order by id ASC";
            WLMClassBinder classBinder = new WLMClassBinder(ref ConexionDB);
            this.GetRecord(queryString);
        }

        public MiWebSiteDomain(int registryId,ref dbConnector ConexionDB)
        {
            ConexionDBInterna = ConexionDB;
            string queryString = "";
            queryString = "SELECT * FROM DOMINIOS WHERE id='" + registryId + "'";
            WLMClassBinder classBinder = new WLMClassBinder(ref ConexionDB);
            this.GetRecord(queryString);
        }
        #endregion

        #region public methods

        public void Delete()
        {
            string queryString = "";
            queryString = "DELETE FROM DOMINIOS WHERE id='" + id + "'";
            ConexionDBInterna.ExecuteCommand(queryString);

        }
        #endregion


        #region Private Methods
        private void GetRecord(string queryString)
        {
            OdbcDataReader reader = ConexionDBInterna.ExecuteReader(queryString);
            if (reader.HasRows)
            {
                if (!reader.IsDBNull(reader.GetOrdinal("id")))
                    id = (System.Int32)reader["id"];
                if (!reader.IsDBNull(reader.GetOrdinal("fechaCreacion")))
                    fechaCreacion = Convert.ToDateTime(reader["fechaCreacion"]);  if (!reader.IsDBNull(reader.GetOrdinal("fechaModificacion"))) fechaModificacion = Convert.ToDateTime(reader["fechaModificacion"]); if (!reader.IsDBNull(reader.GetOrdinal("idUsuarioModificacion"))) idUsuarioModificacion = (System.Int32)reader["idUsuarioModificacion"];
                
                    
                if (!reader.IsDBNull(reader.GetOrdinal("idUsuario")))
                    idUsuario = (System.Int32)reader["idUsuario"];
                if (!reader.IsDBNull(reader.GetOrdinal("dominio")))
                    _dominio = (System.String)reader["dominio"];
                if (!reader.IsDBNull(reader.GetOrdinal("logotipo")))
                    _logotipo = (System.String)reader["logotipo"];
                if (!reader.IsDBNull(reader.GetOrdinal("slogan")))
                    _slogan = (System.String)reader["slogan"];
                if (!reader.IsDBNull(reader.GetOrdinal("estadisticas")))
                    _estadisticas = (System.String)reader["estadisticas"];
                if (!reader.IsDBNull(reader.GetOrdinal("keywords")))
                    _keywords = (System.String)reader["keywords"];
                if (!reader.IsDBNull(reader.GetOrdinal("description")))
                    _description = (System.String)reader["description"];
                if (!reader.IsDBNull(reader.GetOrdinal("mapPoint")))
                    _mapPoint = (System.String)reader["mapPoint"];
                if (!reader.IsDBNull(reader.GetOrdinal("mapsApiKey")))
                    _mapsApiKey = (System.String)reader["mapsApiKey"];
                if (!reader.IsDBNull(reader.GetOrdinal("twitterAccount")))
                    _twitterAccount = (System.String)reader["twitterAccount"];
                if (!reader.IsDBNull(reader.GetOrdinal("twitterPassword")))
                    _twitterPassword = (System.String)reader["twitterPassword"];
                if (!reader.IsDBNull(reader.GetOrdinal("idDomain")))
                    _idDomain = (System.Int32)reader["idDomain"];
                if (!reader.IsDBNull(reader.GetOrdinal("idParentDomain")))
                    _idParentDomain = (System.Int32)reader["idParentDomain"];
                if (!reader.IsDBNull(reader.GetOrdinal("idGrandParentDomain")))
                    _idGrandParentDomain = (System.Int32)reader["idGrandParentDomain"];
                if (!reader.IsDBNull(reader.GetOrdinal("bitlyApiKey")))
                    _bitlyApiKey = (System.String)reader["bitlyApiKey"];
                if (!reader.IsDBNull(reader.GetOrdinal("longName")))
                    _longName = (System.String)reader["longName"];
                if (!reader.IsDBNull(reader.GetOrdinal("activo")))
                    _activo = Convert.ToBoolean(reader["activo"]);
                if (!reader.IsDBNull(reader.GetOrdinal("oauth_token")))
                    _oauth_token = (System.String)reader["oauth_token"];
                if (!reader.IsDBNull(reader.GetOrdinal("oauth_token_secret")))
                    _oauth_token_secret = (System.String)reader["oauth_token_secret"];
                if (!reader.IsDBNull(reader.GetOrdinal("idTwitterApplication")))
                    _idTwitterApplication = (System.Int32)reader["idTwitterApplication"];
            }
        }


        protected override void UpdateBind()
        {
            StringBuilder strCommand = new StringBuilder();
            strCommand.Append("UPDATE DOMINIOS set "); strCommand.Append("fechaModificacion='" + this.fechaModificacion.ToString("yyyy-MM-dd HH:mm:ss") + "'"); strCommand.Append(",idUsuarioModificacion=\"" + idUsuarioModificacion.ToString() + "\"");
            
            strCommand.Append(",dominio=\"" + WLMFunctions.comillas(this._dominio) + "\"");
            strCommand.Append(",logotipo=\"" + WLMFunctions.comillas(this._logotipo) + "\"");
            strCommand.Append(",slogan=\"" + WLMFunctions.comillas(this._slogan) + "\"");
            strCommand.Append(",estadisticas=\"" + WLMFunctions.comillas(this._estadisticas) + "\"");
            strCommand.Append(",keywords=\"" + WLMFunctions.comillas(this._keywords) + "\"");
            strCommand.Append(",description=\"" + WLMFunctions.comillas(this._description) + "\"");
            strCommand.Append(",mapPoint=\"" + WLMFunctions.comillas(this._mapPoint) + "\"");
            strCommand.Append(",mapsApiKey=\"" + WLMFunctions.comillas(this._mapsApiKey) + "\"");
            strCommand.Append(",twitterAccount=\"" + WLMFunctions.comillas(this._twitterAccount) + "\"");
            strCommand.Append(",twitterPassword=\"" + WLMFunctions.comillas(this._twitterPassword) + "\"");
            strCommand.Append(",idDomain='" + this._idDomain.ToString() + "'");
            strCommand.Append(",idParentDomain='" + this._idParentDomain.ToString() + "'");
            strCommand.Append(",idGrandParentDomain='" + this._idGrandParentDomain.ToString() + "'");
            strCommand.Append(",bitlyApiKey=\"" + WLMFunctions.comillas(this._bitlyApiKey) + "\"");
            strCommand.Append(",longName=\"" + WLMFunctions.comillas(this._longName) + "\"");
            strCommand.Append(",activo='" + Convert.ToInt16(this._activo) + "'");
            strCommand.Append(",oauth_token=\"" + WLMFunctions.comillas(this._oauth_token) + "\"");
            strCommand.Append(",oauth_token_secret=\"" + WLMFunctions.comillas(this._oauth_token_secret) + "\"");
            strCommand.Append(",idTwitterApplication='" + this._idTwitterApplication.ToString() + "'");
            strCommand.Append(" WHERE id='" + id + "'");
            ConexionDBInterna.ExecuteCommand(strCommand.ToString());
        }
        #endregion
    }

    public class MiWebSiteDomainCollection
    {
        private dbConnector ConexionDB;
        private BaseClassArray<MiWebSiteDomain> _subdomains;

        public BaseClassArray<MiWebSiteDomain> DomainsList { get { return _subdomains; } }

        public MiWebSiteDomainCollection(ref dbConnector dbConn)
        {
            
            string strCommand = "SELECT id FROM DOMINIOS order by dominio";

            OdbcDataReader reader = ConexionDB.ExecuteReader(strCommand);
            _subdomains = new BaseClassArray<MiWebSiteDomain>();
            while (reader.Read())
            {
                MiWebSiteDomain newEntry = new MiWebSiteDomain(Convert.ToInt32(reader["id"]),ref ConexionDB);

                _subdomains.Add(newEntry);
            }
            reader.Close();
            reader.Dispose();
        }
    }

    public class MiWebSiteCurrentDomain
    {
        #region private fields
        private String _subdomain;
        private String _dominio;
        private String _logotipo;
        private String _slogan;
        private String _estadisticas;
        private String _host;
        private String _keywords;
        protected String _description;
        private int _idDominio;
        #endregion

        #region public properties
        public String subdominio { get { return _subdomain; } set { _subdomain = value; } }
        public String dominio { get { return _dominio; } }
        public String logotipo { get { return _logotipo; } }
        public String slogan { get { return _slogan; } }
        public String estadisticas { get { return _estadisticas; } }
        public String Host { get { return _host; } }
        public String KeyWords { get { return _keywords; } }
        public String Description { get { return _description; } set { _description = value; } }
        public int IdDominio { get { return _idDominio; } }
        #endregion

        public MiWebSiteCurrentDomain(ref dbConnector conn)
        {
            _host = HttpContext.Current.Request.Url.Host;
            MiWebSiteDomainCollection listaDominios = new MiWebSiteDomainCollection(ref conn);
            string dominioActualHost = "";
            foreach (MiWebSiteDomain dominioActual in listaDominios.DomainsList)
            {
                if (_host.Contains(dominioActual.dominio))
                {
                    dominioActualHost = dominioActual.dominio;
                    _dominio = dominioActual.dominio;
                    _logotipo = dominioActual.logotipo;
                    _slogan = dominioActual.slogan;
                    _estadisticas = dominioActual.estadisticas;
                    _keywords = dominioActual.keywords;
                    _description = dominioActual.description;
                    _idDominio = dominioActual.id;
                }
            }
            string[] subDomSeparator = { _dominio };
            _subdomain = _host.Split(subDomSeparator, StringSplitOptions.None)[0].Replace(".", "");
            if (_subdomain == "")
                _subdomain = "www";

        }

    }

    public class MiWebSiteKeyWord : BaseClass
    {
        #region private fields
        private String _keyword;
        #endregion

        #region public properties
        public String keyword { get { return _keyword; } set { _keyword = value; } }
        #endregion

        #region constructors
        public MiWebSiteKeyWord(ref dbConnector ConexionDB)
        {
            
            WLMClassBinder classBinder = new WLMClassBinder(ref ConexionDB);
            id = classBinder.NewRecord("KEYWORDS");
            string queryString = "";
            queryString = "SELECT * FROM KEYWORDS WHERE id=\"" + id + "\"";
            GetRecord(queryString);
        }

        public MiWebSiteKeyWord(int registryId,ref dbConnector ConexionDB)
        {
            
            string queryString = "";
            queryString = "SELECT * FROM KEYWORDS WHERE id='" + registryId + "'";
            WLMClassBinder classBinder = new WLMClassBinder(ref ConexionDB);
            this.GetRecord(queryString);
        }

        public MiWebSiteKeyWord(string word,ref dbConnector ConexionDB)
        {
            
            string queryString = "";
            queryString = "SELECT * FROM KEYWORDS WHERE keyword='" + word + "'";
            WLMClassBinder classBinder = new WLMClassBinder(ref ConexionDB);
            this.GetRecord(queryString);
        }
        #endregion

        #region public methods

        public void Delete()
        {
            string queryString = "";
            queryString = "DELETE FROM KEYWORDS WHERE id=\"" + id + "\"";
            ConexionDBInterna.ExecuteCommand(queryString);

        }
        #endregion

        #region Private Methods
        private void GetRecord(string queryString)
        {
            OdbcDataReader reader = ConexionDBInterna.ExecuteReader(queryString);
            if (reader.HasRows)
            {
                if (!reader.IsDBNull(reader.GetOrdinal("id")))
                    id = (System.Int32)reader["id"];
                if (!reader.IsDBNull(reader.GetOrdinal("fechaCreacion")))
                    fechaCreacion = Convert.ToDateTime(reader["fechaCreacion"]); if (!reader.IsDBNull(reader.GetOrdinal("fechaModificacion"))) fechaModificacion = Convert.ToDateTime(reader["fechaModificacion"]); if (!reader.IsDBNull(reader.GetOrdinal("idUsuarioModificacion"))) idUsuarioModificacion = (System.Int32)reader["idUsuarioModificacion"];


                if (!reader.IsDBNull(reader.GetOrdinal("idUsuario")))
                    idUsuario = (System.Int32)reader["idUsuario"];
                if (!reader.IsDBNull(reader.GetOrdinal("keyword")))
                    _keyword = (System.String)reader["keyword"];
            }
        }

        protected override void UpdateBind()
        {
            StringBuilder strCommand = new StringBuilder();
            strCommand.Append("UPDATE KEYWORDS set "); strCommand.Append("fechaModificacion='" + this.fechaModificacion.ToString("yyyy-MM-dd HH:mm:ss") + "'"); strCommand.Append(",idUsuarioModificacion=\"" + idUsuarioModificacion.ToString() + "\"");

            strCommand.Append(",keyword=\"" + WLMFunctions.comillas(this._keyword) + "\"");
            strCommand.Append(" WHERE id=\"" + id + "\"");
            ConexionDBInterna.ExecuteCommand(strCommand.ToString());
        }
        #endregion
    }

    public class MiWebSiteKeyWordCollection
    {
        private dbConnector ConexionDB;
        private BaseClassArray<MiWebSiteKeyWord> _items;

        public BaseClassArray<MiWebSiteKeyWord> Elementos { get { return _items; } }
        public MiWebSiteKeyWordCollection(ref dbConnector conector)
        {

            string strCommand = "SELECT id FROM KeyWords order by keyword";
            ConexionDB = conector;
            OdbcDataReader reader = ConexionDB.ExecuteReader(strCommand);
            _items = new BaseClassArray<MiWebSiteKeyWord>();
            while (reader.Read())
            {
                MiWebSiteKeyWord item = new MiWebSiteKeyWord(Convert.ToInt32(reader["id"]),ref ConexionDB);

                _items.Add(item);
            }
            reader.Close();
            reader.Dispose();
        }

        public void Save(String listaKeywords)
        {
            ConexionDB.ExecuteCommand("DELETE FROM KEYWORDS");
            string[] arregloKeys = listaKeywords.Split(',');
            foreach (string key in arregloKeys)
            {
                MiWebSiteKeyWord newKeyWord = new MiWebSiteKeyWord(ref ConexionDB);
                newKeyWord.keyword = key.Trim();
                newKeyWord.Save();
                //newKeyWord.Dispose();
            }
        }

        public void GenerarKeywordsApplication()
        {
            StringBuilder salida = new StringBuilder();
            int x = 0;
            foreach (MiWebSiteKeyWord key in Elementos)
            {
                salida.Append(key.keyword);
                if (x < Elementos.Count)
                    salida.Append(", ");
                x++;
            }
            HttpContext.Current.Application["keywords"] = salida.ToString();
        }
    }
}
